package com.agileai.pm.module.base.handler;

import java.util.List;

import com.agileai.common.KeyGenerator;
import com.agileai.domain.DataParam;
import com.agileai.domain.DataRow;
import com.agileai.hotweb.controller.core.PickFillModelHandler;
import com.agileai.hotweb.domain.FormSelectFactory;
import com.agileai.hotweb.renders.AjaxRenderer;
import com.agileai.hotweb.renders.LocalRenderer;
import com.agileai.hotweb.renders.ViewRenderer;
import com.agileai.pm.module.base.service.AddTeamStaff;

public class AddTeamStaffHandler
        extends PickFillModelHandler {
    public AddTeamStaffHandler() {
        super();
        this.serviceId = buildServiceId(AddTeamStaff.class);
    }
    
    public ViewRenderer prepareDisplay(DataParam param){
		mergeParam(param);
		initParameters(param);
		this.setAttributes(param);
		List<DataRow> rsList = getService().queryPickFillRecords(param);
		this.setRsList(rsList);
		processPageAttributes(param);
		return new LocalRenderer(getPage());
	}

    protected void processPageAttributes(DataParam param) {
    	 setAttribute("userType",FormSelectFactory.create("USER_TYPE").
    			 addSelectedValue(getOperaAttributeValue("userType","External")));
    }

    protected void initParameters(DataParam param) {
        initParamItem(param, "USER_NAME", "");
        initMappingItem("USER_TYPE", FormSelectFactory.create("USER_TYPE").getContent());
    }
    public ViewRenderer doSelectTeamStaffAction(DataParam param) {
    	String responseText = FAIL;
    	String userIds = param.getString("USER_ID");
    	String[] idArray = userIds.split(",");
    	for(int i = 0; i < idArray.length; i++){
    		String userId = idArray[i];
    		param.put("USER_ID",userId);
    		String sgId = KeyGenerator.instance().genKey();
    		param.put("PP_ID",sgId);
        	int sort = getService().getMaxSort(param);
    		param.put("PP_SORT",sort);
        	getService().insertPrjectPeople(param);
        	responseText = SUCCESS;
    	}
    	return new AjaxRenderer(responseText);
    }
    protected AddTeamStaff getService() {
        return (AddTeamStaff) this.lookupService(this.getServiceId());
    }
}